package com.lulin.mall.product.controller;

import java.util.Arrays;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.lulin.mall.product.vo.AttrResponseVO;
import com.lulin.mall.product.vo.AttrVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lulin.mall.product.entity.AttrEntity;
import com.lulin.mall.product.service.AttrService;
import com.lulin.common.utils.PageUtils;
import com.lulin.common.utils.R;


/**
 * 商品属性
 *
 * @author lulin
 * @email 2032327049@qq.com
 * @date 2022-09-21 14:25:13
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 根据catelogId，查询规格参数信息
     *
     * @param params    分页的Map对象数据
     * @param catelogId 类目id
     * @return
     */
    //前端请求地址：product/attr/base/list/0?t=1666085174993&page=1&limit=10&key=
    //销售属性：attr/sale/list
    @RequestMapping("/{attrType}/list/{catelogId}")
    public R baseList(@RequestParam Map<String, Object> params,
                      @PathVariable("catelogId") Long catelogId,
                      @PathVariable("attrType") String attrType) {

        PageUtils page = attrService.queryBasePage(params, catelogId, attrType);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId) {
        //AttrEntity attr = attrService.getById(attrId);

        //根据规格参数ID查询对应的详细信息
        AttrResponseVO responseVO = attrService.getAttrInfo(attrId);
        return R.ok().put("attr", responseVO);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVO attrVO) {
        //attrService.save(attr);
        attrService.saveAttr(attrVO);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVO attr) {
        // attrService.updateById(attr);
        //更新基本属性的信息
        attrService.updateBaseAttr(attr);
        return R.ok();
    }

    /**
     * 删除属性
     * ——如果是删除基本属性，还需要删除关联属性表中的数据
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds) {
        //attrService.removeByIds(Arrays.asList(attrIds));
        attrService.removeByIdsDetails(attrIds);
        return R.ok();
    }

}
